home *** CD-ROM | disk | FTP | other *** search
- Path: news.sprintlink.net!datalytics!usenet
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: help with homework
- Date: Thu, 18 Apr 1996 11:21:38 -0400
- Organization: Datalytics, Inc
- Message-ID: <31765E02.16BF@datalytics.com>
- References: <4l1k3i$kle@dfw-ixnews2.ix.netcom.com>
- NNTP-Posting-Host: 204.62.224.71
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- leonel wizel wrote:
- >
- > Please I need help to complete this file: this file suppose to
- > increment the object time by one second to the next hour, to the
- > next minute to the next day:
- >
- > I need some help please:
- >
- > the program suppose to modify to include a tick member function
- > that increments the time stored in the Time object by one second. The
- > time object should always remain in a consistent state. The driver
- > program that tests the tick member function in a loop that prints the
- > time in standard format during each iteration of the loop to illustrate
- > that the tick member functions workds correctly.
- >
- > Incrementing into the next minute.
- > Incrementing into the next hour.
- > incrementing into the next day(i.e., 11:59:59 PM to 12:00:00 AM).
- >
- > //TIME3.H
- >
- > #ifndef TIME3_H
- > #define TIME3_H
- >
- > class Time {
- > public:
- > Time(int = 0, int = 0, int = 0); // constructor
- >
- > //set functions
- >
-
- setTime should probably return a boolean to indicate if the time
- was valid, and only set the time if the input was valid.
-
- > void setTime(int, int, int); //set hour, minute, second
-
- Do you really need the following three member functions? The
- setTime member function seems the most logical, since it can
- handle validating all values at once. setTime could handle
- overflow of any values (like excess seconds increment minute) if
- you like.
-
- > void setHour(int); //set hour
- > void setMinute(int); //set minute
- > void setSecond(int); //set second
- >
- > //get functions
- >
- > int getHour(); //return hour
- > int getMinute(); //return minute
- > int getSecond(); //return second
- >
- > void tick_increment();
-
- It hardly seems appropriate for a Time class to know how to
- print. Rather, it should return a string (or populate a
- supplied buffer) with the formatted time that the class user
- could print, if desired.
-
- Also, consider an enumerated type to indicate the desired
- format:
-
- enum time_format
- {
- MILITARY,
- STANDARD
- };
-
- void format(
- char* o_pBuffer,
- size_t i_BufferLength,
- time_format i_Format);
-
- Of course this doesn't tell the caller how big the buffer needs
- to be, and doesn't indicate if the formatting was successful.
- I'll leave that part to your imagination.
-
- > void printMilitary(); //output military time
- > void printStandard(); //output standard time
- >
- > private:
-
- Why are you using signed types for inherently unsigned values?
- How about unsigned char?
-
- > int hour; //0 - 23
- > int minute; //0 - 59
- > int second; //0 - 59
- > };
- >
- > #endif
- >
- > #include "time3.h"
- > #include <iostream.h>
- >
- > //constructor function
- >
- > Time::Time(int hr, int min, int sec) {setTime (hr, min, sec); }
- >
-
- The action you take here is a little odd. Instead of forcing
- values to zero if the input is invalid, how about return a
- boolean to indicate whether you set the time:
-
- bool Time::setTime(
- unsigned char i_Hour,
- unsigned char i_Minute,
- unsigned char i_Second)
- {
- bool bResult = FALSE;
- if (i_Hour < 24 && i_Minute < 60 && i_Second < 60)
- {
- hour = i_Hour;
- minute = i_Minute;
- second = i_Second;
- bResult = TRUE;
- }
- return bResult;
- }
-
- > void Time::setTime( int h, int m, int s)
- > {
- > hour = (h >= 0 && h < 24) ? h : 0;
- > minute = (m >= 0 && m < 60) ? m : 0;
- > second = (s >= 0 && s < 60) ? s : 0;
- > }
- >
- > void Time::setHour(int h) { hour = (h >= 0 && h < 24) ? h : 0;}
- >
- > void Time::setMinute(int m)
- > { minute = (m >= 60) ? m : 0; }
- >
- > void Time::setSecond(int s)
- > { second = (s >= 60) ? s : 0; }
- >
- > int Time::getHour() {return hour; }
- >
- > int Time::getMinute() {return minute; }
- >
- > int Time::getSecond() {return second; }
- >
- > void Time::tick_increment()
- > {
- > if(second == 60)
- > {
- > second = 0;
- > minute++;
- > }
- > second++;
-
- Doesn't this next part look familiar? Why not increment second
- first, then adjust both second and minute as needed?
- > if (second == 60)
- > {
- > second = 0;
- > minute++;
- > }
- > if(minute == 60)
- > {
- > minute = 0;
- > hour++;
- > }
- > if (hour == 24)
- > {
-
- Modulo works here, but it involves a divide unecessarily. Why
- not just subtract 24?
-
- hour -= 24;
-
- > hour %= 24;
- > }
- > }
-
- Try this instead for tick_increment (wouldn't tick be a better
- name since it's like the tick of a clock?):
-
- void Time::tick(void)
- {
- second++;
- if (second > 59)
- {
- second -= 60;
- minute++;
- }
- if (minute > 59)
- {
- minute -= 60;
- hour++;
- }
- if (hour > 23)
- {
- hour -= 24;
- }
- }
-
- > void Time::printMilitary()
- > {
-
- Don't get so enamored of the ternary operator. Your code is
- less efficient when you insert a null string into cout. Try
- this instead:
-
- if (hour < 10)
- {
- cout << '0';
- }
- cout << hour << ':';
- if (minute < 10)
- {
- cout << '0';
- }
- cout << minute << ':';
- if (second < 10)
- {
- cout << '0';
- }
- cout << second;
-
- When you do this, do you notice how similar all of this code is?
- In other words, we need a member function to capture this
- commonality:
-
- void Time::printNumber(
- int i_Number)
- {
- if (i_Number < 10)
- {
- cout << '0';
- }
- cout << i_Number;
- }
-
- So now, printMilitary becomes this:
-
- printNumber(hour);
- cout << ':';
- printNumber(minute);
- cout << ':';
- printNumber(second);
-
- > cout << (hour < 10 ? "0" : "") << hour << ":"
- > << (minute < 10 ? "0" : "") << minute << ":"
- > << (second < 10 ? "0" : "") << second;
- > }
- >
- > void Time::printStandard()
- > {
- > cout << ((hour == 0 || hour == 12) ? 12 : hour %12) << ":"
- > << (minute < 10 ? "0" : "") << minute << ":"
- > << (second < 10 ? "0" : "") << second
- > << (hour < 12 ? " AM" : " PM");
- > }
- >
- > #include <iostream.h>
- > #include "time3.h"
- > #include "time3.cpp"
- >
- > main()
- > {
- > Time t;
- >
- > t.setTime(23, 59, 59);
- >
- > for(long int i = 1; i <= 2; i++)
- > {
- > cout << endl << endl;
- > cout << " Hour " << t.getHour()
- > << " Minute " << t.getMinute()
- > << " Second " << t.getSecond();
- >
- > cout <<"the initial time before incrementing is: " << endl
- > << endl;
- >
- > t.printStandard();
- > cout << endl;
- > t.printMilitary();
- >
- > t.tick_increment();
- > cout << " Hour " << t.getHour()
- > << " Minute " << t.getMinute()
- > << " Second " << t.getSecond()
- > <<" the time after incrementing is: " << endl;
- >
- > t.printStandard();
- > cout << endl;
- > t.printMilitary();
- >
- > }
- >
- > return 0;
- > }
-
- Having said all that, I have to ask, what's wrong with the code?
- I tested it and it seems to work correctly. Was there something
- specific you noticed that was wrong?
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc. | stew@datalytics.com
-